package gc3d.database;

import java.io.Serializable;
import java.sql.ResultSet;
import java.sql.SQLException;

// https://www.hibernate.org/
public class GridPovDB extends GridDB implements Serializable, Cloneable {
	private static final long serialVersionUID = 1L;

	public GridPovDB(String host, String username, String password) throws ClassNotFoundException {
		super(host, username, password);
	}

	public int createTask(String task_id, String name) {
		return this.update("INSERT INTO tasks VALUES('"+task_id+"','"+name+"','"+now()+"',NULL,NULL,NULL,NULL);");
	}

	public int createJob(String task_id, int frame_id) {
		return this.update("INSERT INTO jobs VALUES("+frame_id+",'"+task_id+"', NULL,NULL,NULL,NULL,NULL,NULL);");
	}

	private boolean isNode(String node_id) {
		String tmp = null;
		ResultSet rs = this.query("SELECT node_id FROM nodes WHERE node_id = '"+node_id+"';");
		try {
			while (rs.next()) {
				tmp = rs.getString("node_id");
			}
		} catch (SQLException e) {
			System.err.println("[ERROR][db] isNode() SQLException "+e.getMessage());
		}
		return node_id.equals(tmp);
	}

	private int createNode(String node_id) {
		int cpu_available = Runtime.getRuntime().availableProcessors();
		String status = System.getProperty("user.name") + " [" + System.getProperty("user.language") 
			+ "] @ " + System.getProperty("os.name") + " [" + System.getProperty("os.arch")+ "] with " 
			+ cpu_available + " available processor";
		if (cpu_available>1) status = status+'s';

		if (isNode(node_id)) {
			return this.update("UPDATE nodes SET last_update='"+now()+"' WHERE node_id = '"+node_id+"';");
		} else {
			return this.update("INSERT INTO nodes VALUES('"+node_id+"','"+now()+"','"+status+"','"+now()+"');");
		}
	}

	public int updateJobStatus(String task_id, int frame_number, String newstatus) {
		return this.update("UPDATE jobs SET status='"+newstatus+"', last_update='"+now()+"' WHERE task_id = '"+task_id+"' AND frame_number = "+frame_number+";");
	}
	public int startJob(String task_id, int frame_number, String node_id) {
		this.createNode(node_id);
		return this.update("UPDATE jobs SET start_time='"+now()+"',node_id='"+node_id+"' WHERE task_id = '"+task_id+"' AND frame_number = "+frame_number+";");
	}
	public int finishJob(String task_id, int frame_number, String frame_link) {
		return this.update("UPDATE jobs SET finish_time='"+now()+"',frame_link='"+frame_link+"' WHERE task_id = '"+task_id+"' AND frame_number = "+frame_number+";");
	}

	public int updateNodeStatus(String node_id, String newstatus) {
		return this.update("UPDATE nodes SET status='"+newstatus+"', last_update='"+now()+"' WHERE node_id = '"+node_id+"';");
	}

	public int updateTaskStatus(String task_id, String newstatus) {
		return this.update("UPDATE tasks SET status='"+newstatus+"', last_update='"+now()+"' WHERE task_id = '"+task_id+"';");
	}
	public int finishTask(String task_id, String video_link) {
		return this.update("UPDATE tasks SET finish_time='"+now()+"',video_link='"+video_link+"' WHERE task_id = '"+task_id+"';");
	}

	@Override
	public GridPovDB clone() {
		return (GridPovDB) super.clone();
	}
}
